package com.example.clothingmanager.service;

import com.example.clothingmanager.bean.Clothes;
import com.example.clothingmanager.dao.ClothesDao;
import com.example.clothingmanager.dao.InstockDao;
import com.example.clothingmanager.dao.OutstockDao;
import com.example.clothingmanager.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author Huangyt
 * @version 1.0
 * @date 2020/5/7 16:51
 */
@Service
public class ClothesService {
    @Autowired
    ClothesDao clothesDao;
    @Autowired
    InstockDao instockDao;
    @Autowired
    OutstockDao outstockDao;

    public Page<Clothes> list(int page, int size, String keyword){
        if(page<1){
            page = 1;
        }
        page -= 1;
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "citem");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        if("".equals(keyword)){
            Page<Clothes> list = clothesDao.findAll(pageable);
            countStock(list);
            return list;
        }
        return clothesDao.findByKeyword(keyword, pageable);
    }

    public Result add(Clothes clothes){
        if(!exit(clothes.getCitem())){
            clothes.setStock(0);
            return Result.success(clothesDao.saveAndFlush(clothes));
        }
        return Result.fail("服装已存在，添加失败");
    }

    public Result update(Clothes clothes){
        if(exit(clothes.getCitem())){
            return Result.success(clothesDao.saveAndFlush(clothes));
        }
        return Result.fail("服装不存在，更新失败");
    }

    public Result delete(String citem){
        if(exit(citem)){
            clothesDao.deleteById(citem);
            return Result.success();
        }
        return Result.fail("服装不存在，删除失败");
    }

    public Result get(String citem){
        Optional<Clothes> optional = clothesDao.findById(citem);
        if(optional.isPresent()){
            Clothes clothes = optional.get();
            return Result.success(clothes);
        }
        return Result.fail("ID不存在，获取失败");
    }

    public boolean exit(String citem){
        Optional<Clothes> optional = clothesDao.findById(citem);
        if(optional.isPresent()){
            return true;
        }
        return false;
    }

    public Result clothesList(){
        List<Clothes> list = clothesDao.findAll();
        if(list.size()>0){
            return Result.success(list);
        }
        return Result.fail("服装列表为空");
    }

    public Result save(Clothes clothes){
        if(null!=clothes){
            clothesDao.saveAndFlush(clothes);
            return Result.success();
        }
        return Result.fail("保存对象为空");
    }

    public void countStock(Page<Clothes> page){
        Integer instock = 0;
        Integer outstock = 0;
        for(Clothes clothes : page.getContent()){
            String citem = clothes.getCitem();
            instock = instockDao.countByClothes(citem);
            if(null==instock){
                instock = 0;
            }
            outstock = outstockDao.countByClothes(citem);
            if(null==outstock){
                outstock = 0;
            }
            clothes.setStock(instock-outstock);
        }
    }
}
